package com.nstar.base.util
{
	import mx.collections.ArrayCollection;
	
	public class ArrayUtils
	{
		/**
		 * 在ArrayCollection里通过某个属性值找到该item的index
		 **/
		public static function searchItem(array:ArrayCollection, search:String):int {
			for(var i:int=0;i<array.length;i++) {
				var item:Object = array.getItemAt(i);
				for each(var element:Object in item) {
					if(element == search) 
						return i;
				}
			}
			
			return -1;
		}
		
		
		/**
		 * 在Array里通过某个属性值找到该item的index
		 **/
		public static function searchItemLabel(array:Array,propertyName:String, value:String):int {
			for(var i:int=0;i<array.length;i++) {
				var item:Object = array[i];
				if(getProperty(item, propertyName) == value) {
					return i;
				}
			}
			
			return -1;
		}
		
		/**
		 * 在ArrayCollection里通过某个属性值找到该item的index
		 **/
		public static function searchCollectionItem(array:ArrayCollection,propertyName:String, value:*):int {
			for(var i:int=0;i<array.length;i++) {
				var item:Object = array.getItemAt(i);
				if(getProperty(item, propertyName) == value) {
					return i;
				}
			}
			
			return -1;
		}
		
		/**
		 * 
		 * 递归查找XMLList某个元素，并返回
		 */
		public static function getXMLListElement(root:XMLList, dataLabel:String, data:String):XML {
				for each(var element:XML in root) {	
					if(element[dataLabel] == data) 
						return element;
					else {
						var r = getXMLListElement(element.node, dataLabel, data);
						if ( r!== null)
							return r;
					}	
					
				}
				
				return null;
				
		}
		
		/**
		 * 将Array 转为 XMLList
		 */
		public static function encodeTree(array:Array, id:String, parentId:String, 
			data:String, label:String):XMLList {
			var root:XML = <root/>;
			trace(root.toXMLString());
			if(array.length == 0)	return null;
			

			var setlist:ArrayCollection = new ArrayCollection();
			var getlist:ArrayCollection = new ArrayCollection(array);
			

				
			for(var i:int=0; i<getlist.length; i++) {
				var temp:Object = getlist.getItemAt(i);
				//如果接点已经被添加到树，就继续往下走
				if(searchItem(setlist, temp[label]) != -1)
						continue;
				//当前接点的父id
				var parentData:String = getProperty(temp,parentId);
					
				if(parentData == null || parentData == "") {
					var node:XML = <node/>;
					node.@label = getProperty(temp,label);
					node.@data = getProperty(temp, data);
					root.appendChild(node);
					setlist.addItem(temp);
				}
				else {
					for(var n:int = 0; n< setlist.length; n++) {
						var setTemp = setlist.getItemAt(n);
						var idData:String =  getProperty(setTemp,id);
						if(idData == parentData) {
							setlist.addItem(temp);
								
							var node:XML = <node/>;
							node.@label = getProperty(temp,label);
							node.@data = getProperty(temp, data);
							
							getXMLListElement(root.node, "@data", idData).appendChild(node);

								
							break;
						}
					}
				}
			}

			//trace(root.toXMLString());

			return root.node;
		}
		
		/**济南 为班组写
		 * 20070905
		 * 将Array 转为 XMLList
		 */
		public static function encodeTree1(array:Array, id:String, parentId:String, 
			data:String, label:String,level:String ):XMLList {
			var root:XML = <root/>;
			trace(root.toXMLString());
			if(array.length == 0)	return null;
			

			var setlist:ArrayCollection = new ArrayCollection();
			var getlist:ArrayCollection = new ArrayCollection(array);
			var maxLevel:String="";
			var tmpLevel:String="";
			
			for(var i:int=0; i<getlist.length; i++) {
				var tmp:Object = getlist.getItemAt(i);
				
				tmpLevel = getProperty(tmp,level);
				if ( i==0 ) {
					maxLevel = getProperty(tmp,level);
				}
				if ( tmpLevel < maxLevel ) {
					maxLevel = tmpLevel;
				}
			}

			 
			for(var i:int=0; i<getlist.length; i++) {
				
				 
				var temp:Object = getlist.getItemAt(i);
				//如果接点已经被添加到树，就继续往下走
				if(searchItem(setlist, temp[label]) != -1)
						continue;
				//当前树的级别
				var nowLevel:String = getProperty(temp,level);
				var parentData:String = getProperty(temp,parentId);
					
				if( nowLevel == maxLevel  ) {
					var node:XML = <node/>;
					node.@label = getProperty(temp,label);
					node.@data = getProperty(temp, data);
					root.appendChild(node);
					setlist.addItem(temp);
				}
				else {
					for(var n:int = 0; n< setlist.length; n++) {
						var setTemp = setlist.getItemAt(n);
						var idData:String =  getProperty(setTemp,id);
						if(idData == parentData) {
							setlist.addItem(temp);
							var node:XML = <node/>;
							node.@label = getProperty(temp,label);
							node.@data = getProperty(temp, data);
							getXMLListElement(root.node, "@data", idData).appendChild(node);
							break;
						}
					}
				}
			}
			 
			//trace(root.toXMLString());

			return root.node;
		}
		
		
		
		
		
		/**济南 为班组写
		 * 20070905  转换不规则的树
		 * 将Array 转为 XMLList
		 */
		public static function encodeTree2(array:Array, id:String, parentId:String, 
			data:String, label:String,level:String ):XMLList {
			var root:XML = <root/>;
			trace(root.toXMLString());
			if(array.length == 0)	return null;
			
            var parentList:ArrayCollection = new ArrayCollection();
			var setlist:ArrayCollection = new ArrayCollection();
			var getlist:ArrayCollection = new ArrayCollection(array);
			var maxLevel:String="";
			var tmpLevel:String="";
			var tmpId:String="";
			
			//取得了所有没有父亲的接点
			for(var i:int=0; i<getlist.length; i++) {
				var tmp:Object = getlist.getItemAt(i);
				//当前接点
				tmpId = getProperty(tmp,parentId);
				//判断它是否有父接点，没有就加入到parentList
				for(var j:int=0; j<getlist.length; j++) {
					var obj:Object = getlist.getItemAt(j);
					var parent:String = getProperty(obj,id);
					if ( tmpId ==  parent ) {
						break;
					}
					if ( j == getlist.length-1 ) {
						parentList.addItem(tmp);
					}
				}
			 }
            
			 
			for(var i:int=0; i<parentList.length; i++) {
				
				 
				var temp:Object = parentList.getItemAt(i);
				//如果接点已经被添加到树，就继续往下走
				if(searchItem(setlist, temp[label]) != -1)
						continue;
				//当前树的级别
				var nowLevel:String = getProperty(temp,level);
				var parentData:String = getProperty(temp,parentId);
					
				
				var node:XML = <node/>;
				node.@label = getProperty(temp,label);
				node.@data = getProperty(temp, data);
				root.appendChild(node);
				setlist.addItem(temp);
				
			
				for(var n:int = 0; n< setlist.length; n++) {
					var setTemp = setlist.getItemAt(n);
					var idData:String =  getProperty(setTemp,id);
					if(idData == parentData) {
						setlist.addItem(temp);
						var node:XML = <node/>;
						node.@label = getProperty(temp,label);
						node.@data = getProperty(temp, data);
						getXMLListElement(root.node, "@data", idData).appendChild(node);
						break;
					}
				}
			
			}
			 
			//trace(root.toXMLString());

			return root.node;
		}
		
		
		
		
		
		
		
		/**
		 * 得到对象的属性，支持“.”操作
		 * 
		 **/
		public static function getProperty(obj:Object, property:String):String {
			var properties:Array = property.split(".");
			var temp:Object = obj;
			for(var i:int=0; i<properties.length; i++) {
				if(temp == null)
					return null;
				
				temp = temp[properties[i]];
			}
			//返回了父Id
			return String(temp);
		}
		
		
		public static function transCollection(array:Array, data:String, label:String):ArrayCollection {
			if(array.length == 0)	return null;
			
			var setlist:ArrayCollection = new ArrayCollection();
			var getlist:ArrayCollection = new ArrayCollection(array);
			
			for(var i:int=0; i<getlist.length; i++) {
				var temp:Object = getlist.getItemAt(i);
				/* var newItem;
				newItem["data"] = temp[data];
				newItem["label"] = temp[label]; */
				setlist.addItem({"data":temp[data],"label":temp[label]});
			}
			
			return setlist;
		}
		
		/**
		 * 将XMLList转为ArrayCollection
		 **/
		public static function xmlToArrayCollection(source:XMLList, 
			label:String = "label", data:String = "data"):ArrayCollection {
			
			function addElement(element:XML): void 
			{
				array.addItem({data:element.attribute(data),label:element.attribute(label)});
				
				if(element.elements().length() != 0) {
					for each ( var e2:XML in element.elements() ) {
						addElement(e2);
					}
				}
			}
			
			var root:XML = <root>{source}</root>
			var array:ArrayCollection = new ArrayCollection();
			
			if(root.elements().length() != 0) {
				for each ( var e1:XML in root.elements() ) {
					addElement(e1);
				}
			}
			
			
			return array;
		}
		
		
	}
	
	
	
}